package com.cms.admin.service.common.impl;

import com.cms.admin.mapper.AdvertisementListDataMapper;
import com.cms.admin.mapper.AdvertisementListMapper;
import com.cms.admin.model.AdvertisementList;
import com.cms.admin.model.AdvertisementListData;
import com.cms.admin.model.AdvertisementListExt;
import com.cms.admin.model.PageBean;
import com.cms.admin.service.common.AdvertisementListService;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by YYY-Work on 2017/5/4.
 */
public class AdvertisementListServiceImpl implements AdvertisementListService{

    private AdvertisementListMapper advertisementListMapper;
    private AdvertisementListDataMapper advertisementListDataMapper;
    
    @Override
    public List<AdvertisementList> queryAdvertisementList(PageBean page, AdvertisementListExt example) {
        int count=getRowCount(example);
        page.setAllRow(count);
        example.setOffset(page.getOffset());
        example.setPageSize(page.getPageSize());
        return advertisementListMapper.selectByConditionWithLimit(example);
    }

    @Override
    public int getRowCount(AdvertisementListExt example) {
        return advertisementListMapper.countByCondition(example);
    }

    @Override
    public int addAdvertisement(AdvertisementList record) {
    	Long advertisementid =advertisementListMapper.selectAdvertisementid();
    	
    	System.out.println(advertisementid);
    	record.setAdvertisementid(advertisementid);
        return advertisementListMapper.insertSelective(record);
    }
    
	@Override
	public int addAdvertisement(AdvertisementList example,
			ArrayList<AdvertisementListData> addAdvertisementListDatas) {
		// TODO Auto-generated method stub
		Long advertisementid =advertisementListMapper.selectAdvertisementid();
    	example.setAdvertisementid(advertisementid);
        advertisementListMapper.insertSelective(example);
        
        if (null!=addAdvertisementListDatas&&addAdvertisementListDatas.size()>0) {
			
			for (int i = 0; i < addAdvertisementListDatas.size(); i++) {
				addAdvertisementListDatas.get(i).setAdvertisementid(advertisementid);
			}
			advertisementListDataMapper.insertBatch(addAdvertisementListDatas);
		}
		
		return 0;
	}

    /**
     * 更新步骤，先删除子表中的数据，然后再批量插入子表
     */
	@Override
	public int modifyAdvertisementList(AdvertisementListExt example,
			ArrayList<AdvertisementListData> addAdvertisementListDatas) {
		
		//广告id 主键
		Long advertisementid = example.getAdvertisementid();
		advertisementListMapper.updateByAdvertisementidSelective(example);
		advertisementListDataMapper.deleteByAdvertisementId(advertisementid);
		
		if (null!=addAdvertisementListDatas&&addAdvertisementListDatas.size()>0) {
			
			for (int i = 0; i < addAdvertisementListDatas.size(); i++) {
				addAdvertisementListDatas.get(i).setAdvertisementid(advertisementid);
			}
			advertisementListDataMapper.insertBatch(addAdvertisementListDatas);
		}
		
		// TODO Auto-generated method stub
		return 0;
	}
    
    @Override
    public AdvertisementList queryAdvertisementListById(Long advertisementid) {
        return advertisementListMapper.selectByAdvertisementid(advertisementid);
    }

    @Override
    public int modifyAdvertisementList(AdvertisementListExt example) {
        return advertisementListMapper.updateByAdvertisementidSelective(example);
    }

    public AdvertisementListMapper getAdvertisementListMapper() {
        return advertisementListMapper;
    }

    public void setAdvertisementListMapper(AdvertisementListMapper advertisementListMapper) {
        this.advertisementListMapper = advertisementListMapper;
    }

	public AdvertisementListDataMapper getAdvertisementListDataMapper() {
		return advertisementListDataMapper;
	}

	public void setAdvertisementListDataMapper(
			AdvertisementListDataMapper advertisementListDataMapper) {
		this.advertisementListDataMapper = advertisementListDataMapper;
	}



}
